package scsc.web.process.actions;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jbpm.JbpmContext;
import org.jbpm.db.GraphSession;
import org.jbpm.db.TaskMgmtSession;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.taskmgmt.exe.TaskInstance;

import scsc.pm.SysTaskItem;
import scsc.pub.data.UserContext;
import scsc.pub.db.DBManage;
import scsc.pub.exception.UnifyUserException;
import scsc.pub.tools.StringUtil;
import scsc.web.process.forms.LeaveForm;
import scsc.web.system.AppAction;
import scsc.web.system.Constants;
import scsc.web.system.forms.ProcessForm;

public class LeaveAction extends AppAction {
	public LeaveAction() throws NamingException {
		super();
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		LeaveForm actionForm = (LeaveForm) form;
		String fwd = null;
		ActionErrors errors = new ActionErrors();
		HttpSession session = request.getSession();
		UserContext userContext = (UserContext) session
				.getAttribute("userContext");

		// data validating process start...
		// fwd = validate(request, userContext, transactLocal, actionForm,
		// errors);

		if (!errors.isEmpty() && fwd != null) {

			saveErrors(request, errors);
			return mapping.findForward(fwd);
		}
		if (fwd != null) {

			return mapping.findForward(fwd);
		}
		// data validating process end.

		// operate DB process start...
		// operAtt can equal one of
		// "create","modify","select","queryItem","del","add","edit"
		String operAtt = actionForm.getOperAtt();

		// if operAtt is null or empty string,set the value "select"
		if (operAtt == null || StringUtil.trim(operAtt).equals("")) {

			operAtt = Constants.DB_Select;
			actionForm.setOperAtt(operAtt);

		}

		/*
		 * String destination = getDestination(operAtt); String destinationUrl =
		 * mapping.findForward(destination).getPath(); try { if
		 * (!checkPermission(userContext, destinationUrl)) {
		 * errors.add("ProcessSession", new ActionError(
		 * Constants.HasNotPermission)); saveErrors(request, errors); return
		 * mapping.findForward(Constants.GlobalHasNotPermission); } } catch
		 * (UnifyUserException e) { errors.add("ProcessSession", new
		 * ActionError( Constants.HasNotPermission)); saveErrors(request,
		 * errors); return
		 * mapping.findForward(Constants.GlobalHasNotPermission); }
		 */
		JbpmContext jbpmContext = JbpmContext.getCurrentJbpmContext();
		GraphSession graphSession = jbpmContext.getGraphSession();
		TaskMgmtSession taskMgmtSession = jbpmContext.getTaskMgmtSession();

		long taskInstanceId = actionForm.getTaskInstanceId();
		Long taskItemInstanceId=new Long(actionForm.getTaskItemInstanceId());
		jbpmContext.setActorId(userContext.getOperatorData().getOperatorid());
		if (operAtt.equals("requestSave")) {
			String dates = actionForm.getDates();
			TaskInstance taskInstance = taskMgmtSession
					.loadTaskInstance(taskInstanceId);
			taskInstance.setVariable("dates", new Integer(dates));
			SysTaskItem taskItem=new SysTaskItem();
			DBManage dbmanage=new DBManage();
			dbmanage.begin();
			try {
				taskItem.isDone(dbmanage,taskItemInstanceId);
				dbmanage.commit();
			} catch (UnifyUserException e) {
				dbmanage.rollback();
			}
			dbmanage.close();
			
			response.setContentType("text/html; charset=GBK");
			response.sendRedirect("/process.do?operAtt=edit&taskInstanceId="
					+ taskInstanceId);
			return null;
			// fwd = Constants.Process_Perform_Create_Success_Forward;
			// fwd = login(userName, password, errors, request, response);

		} else if (operAtt.equals("cancelSave")) {
			String cancel = actionForm.getCancel();
			TaskInstance taskInstance = taskMgmtSession
					.loadTaskInstance(taskInstanceId);
			taskInstance.setVariable("cancel", cancel);
			
			SysTaskItem taskItem=new SysTaskItem();
			DBManage dbmanage=new DBManage();
			dbmanage.begin();
			try {
				taskItem.isDone(dbmanage,taskItemInstanceId);
				dbmanage.commit();
			} catch (UnifyUserException e) {
				dbmanage.rollback();
			}
			dbmanage.close();
			
			response.setContentType("text/html; charset=GBK");
			response.sendRedirect("/process.do?operAtt=edit&taskInstanceId="
					+ taskInstanceId);
			return null;
		} else if (operAtt.equals("chiefSave")) {
			String chief = actionForm.getChief();
			TaskInstance taskInstance = taskMgmtSession
					.loadTaskInstance(taskInstanceId);
			taskInstance.setVariable("chief", chief);
			
			SysTaskItem taskItem=new SysTaskItem();
			DBManage dbmanage=new DBManage();
			dbmanage.begin();
			try {
				taskItem.isDone(dbmanage,taskItemInstanceId);
				dbmanage.commit();
			} catch (UnifyUserException e) {
				dbmanage.rollback();
			}
			dbmanage.close();
			
			response.setContentType("text/html; charset=GBK");
			response.sendRedirect("/process.do?operAtt=edit&taskInstanceId="
					+ taskInstanceId);
			return null;
		} else if (operAtt.equals("bossSave")) {
			String boss = actionForm.getBoss();
			TaskInstance taskInstance = taskMgmtSession
					.loadTaskInstance(taskInstanceId);
			taskInstance.setVariable("boss", boss);
			
			SysTaskItem taskItem=new SysTaskItem();
			DBManage dbmanage=new DBManage();
			dbmanage.begin();
			try {
				taskItem.isDone(dbmanage,taskItemInstanceId);
				dbmanage.commit();
			} catch (UnifyUserException e) {
				dbmanage.rollback();
			}
			dbmanage.close();
			
			response.setContentType("text/html; charset=GBK");
			response.sendRedirect("/process.do?operAtt=edit&taskInstanceId="
					+ taskInstanceId);
			return null;
		} else {
			fwd = operAtt;
		}

		if (!errors.isEmpty()) {
			saveErrors(request, errors);
		}

		ActionForward forward = mapping.findForward(fwd);

		return forward;
	}

}
